﻿using UnityEngine;
using System.Collections;

using Sfs2X;
using Sfs2X.Core;
using Sfs2X.Requests;
using Sfs2X.Entities;
using Sfs2X.Entities.Data;
using Sfs2X.Logging;

/// <summary>
/// Smartfox Network Base Class
/// </summary>
[AddComponentMenu("wsLibrary/Network/Smartfox Network Base Class (do not use this directly)")]
public class wsSmartfoxNetClient : MonoBehaviour
{
	/// <summary>
	/// instance of smartfox network manager.
	/// </summary>
	protected wsSmartfoxNetManager smartfoxManager;

	void Awake()
	{
		smartfoxManager = wsSmartfoxNetManager.Instance;

		smartfoxManager.OnConnectionEvent += OnConnectionEvent;
		smartfoxManager.OnConnectionLostEvent += OnConnectionLostEvent;
		smartfoxManager.OnLoginEvent += OnLoginEvent; ;
		smartfoxManager.OnLoginErrorEvent += OnLoginErrorEvent;
		smartfoxManager.OnLogoutEvent += OnLogoutEvent;
		smartfoxManager.OnCreateRoomEvent += OnCreateRoomEvent;
		smartfoxManager.OnJoinRoomEvent += OnJoinRoomEvent;
		smartfoxManager.OnPublicMessageEvent += OnPublicMessageEvent;
		smartfoxManager.OnExtensionResponseEvent += OnExtensionResponseEvent;
	}

	void Start()
	{
		Init();
	}

	/// <summary>
	/// init method when game start.
	/// </summary>
	protected virtual void Init()
	{
	}

	/// <summary>
	/// log message
	/// </summary>
	/// <param name="msg"></param>
	protected virtual void Log(string msg)
	{
		smartfoxManager.trace(msg);
	}

	/// <summary>
	/// OnConnectionEvent
	/// </summary>
	/// <param name="success"></param>
	protected virtual void OnConnectionEvent(bool success)
	{
	}

	/// <summary>
	/// OnConnectionLostEvent
	/// </summary>
	protected virtual void OnConnectionLostEvent()
	{
	}

	/// <summary>
	/// OnLoginEvent
	/// </summary>
	/// <param name="user"></param>
	protected virtual void OnLoginEvent(Sfs2X.Entities.User user)
	{
	}

	/// <summary>
	/// OnLoginErrorEvent
	/// </summary>
	protected virtual void OnLoginErrorEvent()
	{
	}

	/// <summary>
	/// OnLogoutEvent
	/// </summary>
	/// <param name="user"></param>
	protected virtual void OnLogoutEvent(Sfs2X.Entities.User user)
	{
	}

	/// <summary>
	/// OnCreateRoomEvent
	/// </summary>
	/// <param name="room"></param>
	protected virtual void OnCreateRoomEvent(Sfs2X.Entities.Room room)
	{
	}

	/// <summary>
	/// OnJoinRoomEvent
	/// </summary>
	/// <param name="room"></param>
	protected virtual void OnJoinRoomEvent(Sfs2X.Entities.Room room)
	{
		// send extension message.
		//		ISFSObject outData = new SFSObject();
		//		outData.PutNull("noParameterMsg");
		//		smartfoxManager.Send(new ExtensionRequest("Test", outData));
	}

	/// <summary>
	/// OnPublicMessageEvent
	/// </summary>
	/// <param name="user"></param>
	/// <param name="msg"></param>
	protected virtual void OnPublicMessageEvent(Sfs2X.Entities.User user, string msg)
	{
	}

	/// <summary>
	/// OnExtensionResponseEvent
	/// </summary>
	/// <param name="cmd"></param>
	/// <param name="inData"></param>
	protected virtual void OnExtensionResponseEvent(string cmd, Sfs2X.Entities.Data.ISFSObject inData)
	{
		//		if( cmd == "Test" )
		//		{
		//			this.Log("getMessage is " + inData.GetInt("result"));
		//		}
	}
}
